package com.example.demo.controller;

import com.example.demo.base.BaseResponse;
import com.example.demo.base.ComStatusCodeEnum;
import com.example.demo.base.Filter;
import com.example.demo.base.QueryBuilder;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Validated
@RequestMapping({"/manage/"})
public class LoginController{

    @Autowired
    private UserService userService;

    @PostMapping(value = "/user/login")
    public BaseResponse login(
            @RequestParam(required = false, value = "platForm") String platForm,
            @RequestParam(required = false, value = "passWord") String passWord,
            HttpServletRequest request){
        QueryBuilder queryBuilder = new QueryBuilder();
        queryBuilder.addFilter(Filter.eq("plat_form",platForm))
                    .addFilter(Filter.eq("password",StringUtils.MD5Encode(passWord)));
        User user = userService.selectEntity(queryBuilder);
        if(user==null){
            return BaseResponse.error();
        }
        if(user.getState()==0){
            return BaseResponse.success();
        }else{
            return new BaseResponse(ComStatusCodeEnum.COM_YZ_SUCCESS.getCode(),user.getId().toString());
        }

    }
}
